package com.example.april.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.example.april.service.OAuth2Service;
import com.example.april.utils.HttpUtils;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;

@Service
public class OAuth2ServiceImpl implements OAuth2Service {

    @Value("${OAuth.gitee-client-id}")
    String gitee_client_id;
    @Value("${OAuth.gitee-redirect-uri}")
    String gitee_redirect_uri;

    @Value("${OAuth.gitee-client-secret}")
    String gitee_client_secret;

    @Value("${OAuth.gitHub-client-id}")
    String github_client_id;
    @Value("${OAuth.gitHub-client-secret}")
    String github_client_secret;

    @Override
    public Map<String,String> giteeCallback(String code) {
        HashMap<String, String> body = new HashMap<>();
        body.put("client_id", gitee_client_id);
        body.put("grant_type", "authorization_code");
        body.put("code", code);
        body.put("redirect_uri", gitee_redirect_uri);
        body.put("client_secret", gitee_client_secret);

        String token = getToken("https://gitee.com", "/oauth/token", new HashMap<>(), body);

        Map<String, String> querys = new HashMap();
        querys.put("access_token", token);

        Map<String, String> authorInfo = getAuthorInfo("https://gitee.com","/api/v5/user",new HashMap<String,String>(),querys,"name");

        return authorInfo;
    }

    @Override
    public Map<String,String> githubCallback(String code) {
        HashMap<String, String> body = new HashMap<>();
        body.put("client_id", github_client_id);
        body.put("code", code);
        body.put("accept", "json");
        body.put("client_secret", github_client_secret);
        HashMap<String, String> headers = new HashMap<>();
        headers.put("Accept","application/json");

        String token = getToken("https://github.com", "/login/oauth/access_token", headers, body);

        headers.clear();
        headers.put("Authorization", "Bearer "+token);

        Map<String, String> authorInfo = getAuthorInfo("https://api.github.com", "/user", headers, null, "login");

        return authorInfo;
    }

    public String getToken(String host, String path, Map<String, String> headers, Map<String, String> body) {
        try {
            HttpResponse response = HttpUtils.doPost(host, path, "post", headers, null, body);
            if (response.getStatusLine().getStatusCode() == 200) {
                String json = EntityUtils.toString(response.getEntity());
                JSONObject jsonObject = JSONObject.parseObject(json);
                String access_token = jsonObject.getString("access_token");
                return access_token;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public Map<String,String> getAuthorInfo(String host, String path, Map<String,String> headers, Map<String,String> querys, String name) {
        try {
            HttpResponse res = HttpUtils.doGet(host, path, "get", headers, querys);
            if (res.getStatusLine().getStatusCode() == 200) {
                String json = EntityUtils.toString(res.getEntity());
                JSONObject jO = JSONObject.parseObject(json);
                HashMap<String,String> authorInfo = new HashMap<>();
                authorInfo.put("id",jO.getString("id"));
                authorInfo.put("name",jO.getString(name));
                return authorInfo;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
